package cn.gp.chatclient.model;

import lombok.Data;
import lombok.experimental.Accessors;
import java.util.List;

/**
 * @ClassName ResponseVo
 * @Description TODO
 * @Author He Guoping
 * @Date 2025/6/17 9:45
 * @Version JDK17
 */


@Data
@Accessors(chain = true) // 支持链式调用
public class ResponseVo<T> {
    private boolean success;    // 请求是否成功
    private int code;          // 状态码
    private String message;    // 提示信息
    private T data;            // 响应数据
    private long timestamp;    // 时间戳

    // ---------------------- 成功响应静态方法 ----------------------
    public static <T> ResponseVo<T> success() {
        return new ResponseVo<T>()
                .setSuccess(true)
                .setCode(200)
                .setMessage("OK")
                .setTimestamp(System.currentTimeMillis());
    }

    public static <T> ResponseVo<T> success(T data) {
        return new ResponseVo<T>()
                .setSuccess(true)
                .setCode(200)
                .setMessage("OK")
                .setData(data)
                .setTimestamp(System.currentTimeMillis());
    }

    public static <T> ResponseVo<List<T>> success(List<T> list) {
        return new ResponseVo<List<T>>()
                .setSuccess(true)
                .setCode(200)
                .setMessage("OK")
                .setData(list)
                .setTimestamp(System.currentTimeMillis());
    }

    // ---------------------- 失败响应静态方法 ----------------------
    public static <T> ResponseVo<T> fail(int code, String message) {
        return new ResponseVo<T>()
                .setSuccess(false)
                .setCode(code)
                .setMessage(message)
                .setTimestamp(System.currentTimeMillis());
    }

    // ---------------------- 分页响应 ----------------------
    @Data
    public static class PageResult<T> {
        private List<T> list;     // 当前页数据
        private long total;       // 总条数
        private int pageNum;      // 当前页码
        private int pageSize;     // 每页大小

        public PageResult(List<T> list, long total, int pageNum, int pageSize) {
            this.list = list;
            this.total = total;
            this.pageNum = pageNum;
            this.pageSize = pageSize;
        }
    }

    public static <T> ResponseVo<PageResult<T>> successPage(List<T> list, long total, int pageNum, int pageSize) {
        PageResult<T> pageResult = new PageResult<>(list, total, pageNum, pageSize);
        return ResponseVo.success(pageResult);
    }
}
